import { loginAPI } from '@/api/user'
import { TOKEN_KEY } from '@/constants/KEY'
import { getToken, removeToken, setToken } from '@/utils/auth'
const mutations = {
  setToken(state, token) {
    state.token = token
    setToken(token) // 存本地
  },
  clearUserInfo(state) {
    state.token = null
    removeToken(TOKEN_KEY)
  }
}
const actions = {
  // 调用登录接口
  async loginForm(context, data) {
    const res = await loginAPI(data)
    // 返回一个token
    console.log('xxxx', res)
    context.commit('setToken', res.data.token)
  }
}
export default {
  namespaced: true,
  state: () => {
    return {
      // 优先从本地取
      token: getToken() || ''
    }
  },
  mutations,
  actions
}
